package com.pingba.util.helper;

import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

import java.io.*;
import java.lang.reflect.Field;
import java.util.List;


/**
 * JDom2 Helper2
 * @author Fu Mingjie
 *
 */
public class JDom2Helper {


    /**
     * Add a new attribute
     * @param element Target Element object
     * @param name Attribute name
     * @param value Attribute value
     */
	public static void addAttr(final Element element, final String name, final String value) {
		String v = value == null ? "" : value;
		element.setAttribute(name, v);
	}

	/**
	 * Convert element's attributes to data object's attributes with the same attribute name
	 * @param dataObj Data object
	 * @param element Element element
	 */
	public static void pickup(final Object dataObj, Element element) {
		List<Attribute> attrs = element.getAttributes();
		Class<? extends Object> clazz = dataObj.getClass();
		for (Attribute attr : attrs) {
			Field field = ClazzHelper.getDeclaredField(clazz, attr.getName());
			if (field == null) {
				continue;
			}

			BeanHelper.setValue(dataObj, attr.getName(), attr.getValue());
		}
	}

	/**
	 * Convert element and its children to formated strng.
	 * @param element Element object
	 * @return formated string
	 */
	public static String toString(final Element element) {
		XMLOutputter outer = new XMLOutputter();
		Format format = Format.getPrettyFormat();
		format.setEncoding("utf-8");
		format.setIndent("  ");
		outer.setFormat(format);
		return outer.outputString(element);
	}

	/**
	 * Convert xml str to Jdom document.
	 * @param xmlStr Xml String
	 * @return JDom document
	 */
	public static Document fromStr(final String xmlStr) {
		try {
			SAXBuilder bSAX = new SAXBuilder();
			return bSAX.build(new StringReader(xmlStr));
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * Get dom from file.
	 * @param file XML file
	 * @return Document object
	 */
	public static Document fromFile(final File file) {
	    try {
            return fromInputStream(new FileInputStream(file));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

	    return null;
	}
	/**
	 * Get dom from file.
	 * @param is InputStream
	 * @return Document object
	 */
	public static Document fromInputStream(InputStream is) {
	    SAXBuilder bSAX = new SAXBuilder();
	    try {
            return bSAX.build(is);
        } catch (Exception e) {
            e.printStackTrace();
        }

	    return null;
	}

	/**
	 * Put dom 2 file.
	 * @param file File
	 * @param doc document
	 */
//	public static void toFile(File file, Document doc) {
//		OutputStream os = null;
//	    try {
//			os = new FileOutputStream(file);
//			Format format = Format.getPrettyFormat();
//			format.setEncoding("utf-8");
//			format.setIndent("    ");
//			XMLOutputter output = new XMLOutputter(format);
//			output.output(doc, os);
//			os.flush();
//		} catch (Exception e) {
//			e.printStackTrace();
//		} finally {
//			IoHelper.close(os);
//		}
//	}
}
